from fastapi import APIRouter, HTTPException, Depends
from typing import List
from sqlalchemy.orm import Session
from schema import Server_Status_schema
from controller import Server_Status_controller
from database.db_init import get_db


router = APIRouter()


@router.post("/Server_Status", response_model=Server_Status_schema.Server_status, tags=["Server_Status"])
async def create_Server_Status(Server_Status: Server_Status_schema.Server_status, db: Session = Depends(get_db)):
    return Server_Status_controller.create_Server_Status(db, Server_Status)


@router.get("/Server_Status/{id}", response_model=Server_Status_schema.Server_status, tags=["Server_Status"])
async def get_Server_Status(id: int, db: Session = Depends(get_db)):
    db_Server_Status = Server_Status_controller.get_Server_Status(db, id)
    if db_Server_Status is None:
        raise HTTPException(status_code=404, detail="Server_Status not found")
    return db_Server_Status


@router.get("/Server_Status", response_model=List[Server_Status_schema.Server_status], tags=["Server_Status"])
async def get_all_Server_Status(db: Session = Depends(get_db)):
    all_db_Server_Status = Server_Status_controller.get_all_Server_Status(db)
    if all_db_Server_Status is None:
        raise HTTPException(status_code=404, detail="0 Server_Status found, empty table")
    return all_db_Server_Status


@router.put("/Server_Status/{id}", tags=["Server_Status"])
async def update_Server_Status(id: int, field_name: str, field_value: str, db: Session = Depends(get_db)):
    try:
        db_Server_Status = Server_Status_controller.update_Server_Status(db, id, field_name, field_value)
        if db_Server_Status is None:
            raise HTTPException(status_code=404, detail="Server_Status not found")
        return db_Server_Status
    except ValueError as error:
        raise HTTPException(status_code=400, detail=str(error))


@router.delete("/Server_Status/{id}", tags=["Server_Status"])
async def delete_Server_Status(id: int, db: Session = Depends(get_db)):
    is_deleted = Server_Status_controller.delete_Server_Status(db, id)
    if is_deleted is None:
        raise HTTPException(status_code=404, detail=f'Sample {id} does not exist in database')
    else:
        return {'message': f'Successfully delete sample {id}'}
